package org.springblade.modules.repair.controller;

import cn.hutool.http.HtmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.sms.model.SmsResponse;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.modules.repair.dto.TlwAlarmNotePushDTO;
import org.springblade.modules.repair.dto.TlwAlarmOfficialAccountsPushDTO;
import org.springblade.modules.repair.service.WeChatDialogueService;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * @Date 10:35 2022/10/20
 * @Description {
 *   梯联网消息推送
 * }
 * @Author lsay
 **/
@Slf4j
@RestController
@RequestMapping("/tlw/alarm")
public class TlwAlarmController {

	@Autowired
	private WeChatDialogueService weChatDialogueService;

	@PostMapping("/note/push")
	public R<Boolean> nodePush(@RequestBody TlwAlarmNotePushDTO pushDTO){
		log.info("===========发送梯联网推送短信,手机号码:[{}]===========", pushDTO.getPhone());
		SmsResponse smsResponse = SmsUtil.sendMessage(pushDTO.getSmsCode(), BladeConstant.ADMIN_TENANT_ID,pushDTO.getParams(),pushDTO.getPhone());
		log.info("===========发送梯联网推送短信,发送结果:[{}]===========", smsResponse.toString());
		return R.status(Boolean.TRUE);
	}

	@PostMapping("/official/accounts/push")
	public R<Boolean> officialAccountsPush(@RequestBody TlwAlarmOfficialAccountsPushDTO pushDTO) throws UnsupportedEncodingException {
		log.info("===========发送梯联网推送公众模板消息,OPENID:[{}]===========", pushDTO.getOpenid());
		if (pushDTO.getParams().containsKey("params")) {
			// 处理url转义字符
			String skipUrl = HtmlUtil.unescape(pushDTO.getParams().get("params"));
			pushDTO.getParams().put("params", skipUrl);
			log.info("===========公众模板消息跳转url:{}", skipUrl);
		}
		weChatDialogueService.sendMsg(pushDTO.getOpenid(), pushDTO.getTemplateId(),pushDTO.getParams());
		return R.status(Boolean.TRUE);
	}

}

